<template>
  <el-container class="home-detail-aqjc" v-loading="loading">
    <el-header class="header search-header" style="margin-top: 12px">
      <el-form
        ref="ruleFormRef"
        class="between-form"
        :model="search"
        :inline="true"
      >
        <div>
          <el-form-item label="检查日期" prop="jcrq">
            <el-date-picker
              style="width: 360px"
              v-model="search.jcrq"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="handleTimeChange"
            />
          </el-form-item>
          <el-form-item label="检查类型" prop="inspectionType">
            <el-select
              v-model="search.inspectionType"
              placeholder="请选择"
              style="min-width: 120px"
              clearable
            >
              <el-option key="" label="全部" value="" />
              <el-option
                v-for="item in typeObject.klx"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="zt">
            <el-select
              v-model="search.zt"
              placeholder="请选择"
              style="min-width: 120px"
              clearable
            >
              <el-option key="" label="全部" value="" />
              <el-option
                v-for="item in typeObject.status"
                :key="item.orgId"
                :label="item.orgName"
                :value="item.orgId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="检查人员" prop="inspector">
            <el-input
              v-model="search.inspector"
              placeholder="请输入检查人员"
              clearable
            />
          </el-form-item>
        </div>
        <el-form-item style="margin-right: 0">
          <el-button
            type="primary"
            class="search-btn"
            @click="getSearch(ruleFormRef)"
          >
            查询
          </el-button>
          <el-button
            @click="reset(ruleFormRef)"
            class="reset-btn"
            style="margin-right: 12px"
            >重置</el-button
          >
          <el-button @click="goback" class="reset-btn">返回</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <el-container>
        <el-main>
          <el-container>
            <el-main class="container bc-table">
              <el-table
                :data="dataInfo.tableData"
                style="width: 100%; height: 100%"
              >
                <el-table-column prop="inspectionType" label="检查类型" />
                <el-table-column prop="inspectionDate" label="检查日期" />
                <el-table-column prop="status" label="状态" />
                <el-table-column prop="issuesCount" label="隐患问题数量">
                </el-table-column>
                <el-table-column prop="inspectionDate" label="整改期限" />
                <el-table-column prop="inspector" label="检查人员" />
                <el-table-column label="操作">
                  <template #default="scope">
                    <el-button
                      link
                      type="primary"
                      size="small"
                      @click="gotodetail(scope.row)"
                      >查看</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-main>
            <el-footer class="footer mt20">
              <el-pagination
                v-model:current-page="tableField.pageNo"
                v-model:page-size="tableField.pageSize"
                :page-sizes="[10, 20, 30, 50, 100]"
                layout="sizes, prev, pager, next, jumper"
                :total="total"
                :background="true"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                prev-text="上一页"
                next-text="下一页"
              />
            </el-footer>
          </el-container>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
  <!-- 检查记录详情 -->
  <SelfDialog
    :dialogVisable="addVisable"
    title="检查记录详情"
    width="917px"
    @close="gotodetail()"
    @confirm="gotodetail()"
    :showFooter="false"
  >
    <template #default>
      <div class="center_content">
        <detail :currentRow="currentRowId"></detail>
      </div>
    </template>
  </SelfDialog>
</template>

<script setup>
import { TabbarList } from '@/pinia/tabbar.js';
import { onMounted, ref, reactive, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { paramkvlist, repolist, getTypeList, busioptions } from '@/api';
import { getsalfList } from '@/api/dazx';
import { Minxfun } from '@/utils/minxin';
import SelfDialog from '@/components/dialog/dialog.vue';
import detail from './detail.vue';
import dayjs from 'dayjs';

const {
  handleSizeChange,
  handleCurrentChange,
  tableField,
  total,
  loading,
  formatDateTime,
} = Minxfun();

const emit = defineEmits('close');
const router = useRouter();

const props = defineProps({
  currentData: Object,
});

watch(
  () => props.currentData,
  (newData) => {
    if (newData.id) {
      // getTableData();
    }
  },
  {
    immediate: true,
    deep: true,
  }
);

watch(tableField, (newVal, oldVal) => {
  getTableData();
});

onMounted(() => {
  getType();
  getTableData();
});

// 返回
const goback = () => {
  emit('close');
};

// 获取字典
let typeObject = reactive({
  klx: [],
  cck: [],
  status: [],
  ywfl: [],
});

const getType = async () => {
  try {
    // 库类型
    let res = await paramkvlist({
      paramType: 'belong_module',
    });
    typeObject.klx = res.data;
    let res2 = await getTypeList({
      orgTypeCode: 'office',
    });
    typeObject.status = res2.data;
    let res3 = await busioptions();
    typeObject.ywfl = res3.data;
    // 存储库
    let res1 = await repolist();
    typeObject.cck = res1.data;
  } catch (e) {
    console.error(e);
  }
};

const getSearch = () => {
  if (tableField.value.pageNo == 1) {
    getTableData();
  } else {
    tableField.value.pageNo = 1;
    getTableData();
  }
};

const reset = (ruleFormRef) => {
  ruleFormRef.resetFields();
  search.jcrq = [];
  getTableData();
};

const ruleFormRef = ref();
const search = reactive({
  jcrq: [],
  inspectionType: '',
  status: '',
  inspector: '',
});

let dataInfo = reactive({
  tableData: [],
});

const handleTimeChange = (value) => {
  if (value === null) {
    // 清空时重置为 []
    search.jcrq = [];
  } else {
    search.jcrq = value;
  }
  // 调用getSearch方法来获取数据
  getSearch();
};

// 获取列表
const getTableData = async () => {
  try {
    loading.value = true;
    search.jcrq = search.jcrq || [];
    let { data } = await getsalfList({
      enterpriseType: '2',
      ...search,
      ...tableField.value,
      pageNum: tableField.value.pageNo,
      enterpriseId: props.currentData.id,
      inspectionDateBegin:
        search.jcrq.length > 0
          ? dayjs(search.jcrq[0]).format('YYYY-MM-DD')
          : '',
      inspectionDateEnd:
        search.jcrq.length > 0
          ? dayjs(search.jcrq[1]).format('YYYY-MM-DD')
          : '',
    });
    dataInfo.tableData = data.inspectList;
    total.value = Number(data.total) || 0;
    loading.value = false;
  } catch (e) {
    loading.value = false;
    console.error(e);
  }
};

let currentRow = ref(null);

// 详情
let addVisable = ref(false);
let currentRowId = ref(null);
const gotodetail = (row) => {
  addVisable.value = !addVisable.value;
  if (row) {
    currentRowId.value = row;
  }
};

const handleClick = (value) => {
  currentRow.value = value.id;
  showClint.value = true;
};
</script>

<style lang="scss" scoped>
.home-detail-aqjc {
  width: 100%;
  height: 100%;
  .header {
    min-height: 36px;
    line-height: 36px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 18px;
    .header-l {
      display: flex;
      align-items: center;
      .text {
        font-family: PingFangSC, PingFang SC;
        font-weight: 600;
        font-size: 16px;
        color: #333333;
        line-height: 22px;
        letter-spacing: 1px;
        text-align: left;
        padding-left: 14px;
      }
    }
  }
  .container {
  }
}
.el-container {
  height: 100% !important;
  padding: 0 !important;
}
.el-header {
  height: auto !important;
  min-height: 36px !important;
}
.el-main {
  padding: 0 !important;
}
.search {
  margin: 0 50px;
}
</style>

<style>
.el-form-item__label {
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 14px;
  color: #333333;
}
</style>
